package org.openfidias.jdbase.controller;

import java.io.IOException;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.openfidias.jdbase.locale.Message;
import org.openfidias.jdbase.model.Aluno;
import org.openfidias.jdbase.model.AlunoDao;
import org.openfidias.jdbase.view.Console;

/**
 *
 * @author atila
 */
public class AlunoBo {

   private AlunoBo() {
   }

   public static AlunoBo getInstance() {
      return AlunoBoHolder.INSTANCE;
   }

   private static class AlunoBoHolder {

      private static final AlunoBo INSTANCE = new AlunoBo();
   }

   /**
    * @deprecated
    * @param params
    * @throws IOException
    * @throws Exception
    */
   public void createOrUpdate(Map<String, String> params) throws IOException, Exception {

      // populate and set object
      Aluno aluno = new Aluno();
      if (aluno == null) {
         AlunoDao.getInstance().create(aluno);
      } else {
         AlunoDao.getInstance().update(aluno);
      }
   }

   public void create() throws IOException, Exception {
      Aluno aluno = new Aluno();
      System.out.println(StringUtils.center(" Criar Aluno ", 50, "·"));
      String value = "";

      // setting Login
      while (true) {

         System.out.println("Digite o Login: ");
         System.out.print(Console.bash);
         value = Console.read.nextLine();
         if (StringUtils.isAlpha(value)) {
            if (value.length() > Aluno.maxLogin) {
               System.out.println(Message.outOfBounds("Login", Aluno.maxLogin));
               continue;
            } else if (AlunoDao.getInstance().exists(value)) {
               System.out.println(Message.alreadyExists(AlunoDao.logicalTable, value));
               continue;
            } else {
               aluno.setLogin(value);
               break;
            }
         } else {
            System.out.println(Message.onlyAlpha("Login"));
            continue;
         }
      }

      // setting Nome
      while (true) {
         System.out.println("Digite o Nome: ");
         System.out.print(Console.bash);
         value = Console.read.nextLine();
         if (StringUtils.isAlphaSpace(value)) {
            if (value.length() > Aluno.maxNome) {
               System.out.println(Message.outOfBounds("Nome", Aluno.maxNome));
            } else {
               aluno.setNome(value);
               break;
            }
         } else {
            aluno.setNome(null);
         }
      }

      // setting Matricula
      while (true) {
         System.out.println("Digite a Matrícula: ");
         System.out.print(Console.bash);
         value = Console.read.nextLine();
         if (StringUtils.isNumeric(value)) {
            if (value.length() > Aluno.maxMatricula) {
               System.out.println(Message.outOfBounds("Matrícula", Aluno.maxMatricula));
            } else {
               aluno.setMatricula(Integer.parseInt(value));
               break;
            }
         } else {
            Message.onlyNumeric("Matrícula");
         }
      }

      // saving to the database
      try {
         AlunoDao.getInstance().create(aluno);
      } catch (Exception e) {
         System.out.println(Message.showError(e));
      }
   }

   public void read() {
      System.out.println(StringUtils.center(" Visualizar Aluno ", 50, "·"));
      String value = "";
      System.out.println("Digite o Login: ");
      System.out.print(Console.bash);
      value = Console.read.nextLine();
      try {
         Aluno aluno = AlunoDao.getInstance().read(value);
         System.out.println(Aluno.headerTable());
         System.out.println(aluno);
         System.out.println(Aluno.rowSeparator());
      } catch (Exception e) {
         System.out.println(Message.showError(e));
      }
   }

   public void delete() throws IOException, Exception {
      System.out.println(StringUtils.center(" Deletar Aluno ", 50, "·"));
      String value = "";
      System.out.println("Digite o Login: ");
      System.out.print(Console.bash);
      value = Console.read.nextLine();
      if (!("".equals(value)) && StringUtils.isAlpha(value)) {
         try {
            AlunoDao.getInstance().delete(value);
         } catch (Exception e) {
            System.out.println(Message.showError(e));
         }
      } else {
         System.out.println(Message.onlyAlpha("Login"));
      }
   }
}
